package com.bonc.situation.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.pool.DruidDataSource;

/** 
* @Author guxf
* @Time 2020年12月26日 上午9:57:28 
* @Version 1.0
* <p>Description:</p>
*/

@Configuration
@ConfigurationProperties(prefix = "hive")
public class HiveDruidConfig {
	 private String url;
	    private String user;
	    private String password;
	    private String driverClassName;
	    private int initialSize;
	    private int minIdle;
	    private int maxActive;
	    private int maxWait;
	    private int timeBetweenEvictionRunsMillis;
	    private int minEvictableIdleTimeMillis;
	    private String validationQuery;
	    private boolean testWhileIdle;
	    private boolean testOnBorrow;
	    private boolean testOnReturn;
	    private boolean poolPreparedStatements;
	    private int maxPoolPreparedStatementPerConnectionSize;
	 
	    @Bean(name = "hiveDruidDataSource")
	    @Qualifier("hiveDruidDataSource")
	    public DataSource dataSource() {
	        DruidDataSource datasource = new DruidDataSource();
	        datasource.setUrl(url);
	        datasource.setUsername(user);
	        datasource.setPassword(password);
	        datasource.setDriverClassName(driverClassName);
	        // pool configuration
	        datasource.setInitialSize(initialSize);
	        datasource.setMinIdle(minIdle);
	        datasource.setMaxActive(maxActive);
	        datasource.setMaxWait(maxWait);
	        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
	        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
	        datasource.setValidationQuery(validationQuery);
	        datasource.setTestWhileIdle(testWhileIdle);
	        datasource.setTestOnBorrow(testOnBorrow);
	        datasource.setTestOnReturn(testOnReturn);
	        datasource.setPoolPreparedStatements(poolPreparedStatements);
	        datasource.setMaxPoolPreparedStatementPerConnectionSize(
	            maxPoolPreparedStatementPerConnectionSize);
	        return datasource;
	    }
	 
	    // 此处省略各个属性的get和set方法
	 
	    @Bean(name = "hiveDruidTemplate")
	    public JdbcTemplate hiveDruidTemplate(
	        @Qualifier("hiveDruidDataSource") DataSource dataSource) {
	        return new JdbcTemplate(dataSource);
	    }

}
